<?php

namespace app\admin\model;

use think\Model;


class Order extends Model
{


    // 表名
    protected $name = 'order';

    // 自动写入时间戳字段
    protected $autoWriteTimestamp = 'int';

    // 定义时间戳字段名
    protected $createTime = 'createtime';
    protected $updateTime = 'updatetime';
    protected $deleteTime = false;

    // 追加属性
    protected $append = [
        'good_type_text',
        'status_text',
        'pay_time_text',
        'use_time_text',
        'end_time_text',
        'refund_time_text',
        'validity_time_text',
        'is_discount_text'
    ];


    public function getGoodTypeList()
    {
        return ['1' => __('Good_type 1'), '2' => __('Good_type 2'), '3' => __('Good_type 3'), '4' => __('Good_type 4'), '5' => __('Good_type 5')];
    }

    public function getStatusList()
    {
        return ['-1' => __('Status -1'), '0' => __('Status 0'), '1' => __('Status 1'), '2' => __('Status 2'), '3' => __('Status 3'), '4' => __('Status 4')];
    }

    public function getIsDiscountList()
    {
        return ['0' => __('否'), '1' => __('是')];
    }

    public function getIsDiscountTextAttr($value, $data)
    {
        $value = $value ? $value : (isset($data['is_discount']) ? $data['is_discount'] : '');
        $list  = $this->getIsDiscountList();
        return isset($list[$value]) ? $list[$value] : '';
    }

    public function getGoodTypeTextAttr($value, $data)
    {
        $value = $value ? $value : (isset($data['good_type']) ? $data['good_type'] : '');
        $list  = $this->getGoodTypeList();
        return isset($list[$value]) ? $list[$value] : '';
    }


    public function getStatusTextAttr($value, $data)
    {
        $value = $value ? $value : (isset($data['status']) ? $data['status'] : '');
        $list  = $this->getStatusList();
        return isset($list[$value]) ? $list[$value] : '';
    }


    public function getPayTimeTextAttr($value, $data)
    {
        $value = $value ? $value : (isset($data['pay_time']) ? $data['pay_time'] : '');
        return is_numeric($value) ? date("Y-m-d H:i:s", $value) : $value;
    }


    public function getUseTimeTextAttr($value, $data)
    {
        $value = $value ? $value : (isset($data['use_time']) ? $data['use_time'] : '');
        return is_numeric($value) ? date("Y-m-d H:i:s", $value) : $value;
    }


    public function getEndTimeTextAttr($value, $data)
    {
        $value = $value ? $value : (isset($data['end_time']) ? $data['end_time'] : '');
        return is_numeric($value) ? date("Y-m-d H:i:s", $value) : $value;
    }


    public function getRefundTimeTextAttr($value, $data)
    {
        $value = $value ? $value : (isset($data['refund_time']) ? $data['refund_time'] : '');
        return is_numeric($value) ? date("Y-m-d H:i:s", $value) : $value;
    }


    public function getValidityTimeTextAttr($value, $data)
    {
        $value = $value ? $value : (isset($data['validity_time']) ? $data['validity_time'] : '');
        return is_numeric($value) ? date("Y-m-d H:i:s", $value) : $value;
    }

    protected function setPayTimeAttr($value)
    {
        return $value === '' ? null : ($value && !is_numeric($value) ? strtotime($value) : $value);
    }

    protected function setUseTimeAttr($value)
    {
        return $value === '' ? null : ($value && !is_numeric($value) ? strtotime($value) : $value);
    }

    protected function setEndTimeAttr($value)
    {
        return $value === '' ? null : ($value && !is_numeric($value) ? strtotime($value) : $value);
    }

    protected function setRefundTimeAttr($value)
    {
        return $value === '' ? null : ($value && !is_numeric($value) ? strtotime($value) : $value);
    }

    protected function setValidityTimeAttr($value)
    {
        return $value === '' ? null : ($value && !is_numeric($value) ? strtotime($value) : $value);
    }


    public function user()
    {
        return $this->belongsTo('User', 'user_id', 'id', [], 'LEFT')->setEagerlyType(0);
    }


    public function gymnasium()
    {
        return $this->belongsTo('Gymnasium', 'gymnasium_id', 'id', [], 'LEFT')->setEagerlyType(0);
    }
}
